//
//  RequestManager.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "RequestManager.h"
@interface RequestManager()

@end
@implementation RequestManager
//请求数据，根据类型会自动识别请求方式
+ (void)fetchDataWithUrl:(NSString *)url
               parameter:(NSDictionary *)parameter
                  mehtod:(NSString *)mehtod
                compelet:(RequestCompeleted)compele
                  updata:(UpdataUI)updata{

    //创建请求对象并设置请求对象的属性
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //在子线成里面调用主线程，主线程不能调用主线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        if ([mehtod isEqualToString:@"GET"]) {
            [manager GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                //把Data传出去
                compele((NSData *)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    updata();
                });

            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"%@GET请求失败了",error);
            }];
        }else{
            [manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                
            } progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                //把Data传出去
                compele((NSData *)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    updata();
                });
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"%@POS请求失败了",error);
            }];
        }
        
    });
    
   
}
@end
